package com.blog.job;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blog.constants.SystemConstants;
import com.blog.domain.entity.Article;
import com.blog.service.ArticleService;
import com.blog.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class UpdateViewCountJob {

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private ArticleService articleService;

	@Scheduled(cron = "0/60 * * * * ?")
	public void updateViewCount() {
		//获取redis中的浏览量
		Map<String, Integer> viewCountMap = redisCache.getCacheMap(SystemConstants.REDIS_KEY);

		List<Article> articles = viewCountMap.entrySet()
				.stream()
				.map(entry -> new Article(Long.valueOf(entry.getKey()),
						entry.getValue().longValue()))
				.collect(Collectors.toList());
		//更新到数据库中
		for (Article article : articles) {
			LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
			updateWrapper.eq(Article::getId, article.getId());
			updateWrapper.set(Article::getViewCount, article.getViewCount());
			articleService.update(updateWrapper);
		}
	}
}
